
var username = null;
var password = null;
var remenberMe = null;

/**
 * 解决狗屎浏览器IE不支持trim()函数
 */
String.prototype.trim = function(){
	return this.replace("/^\s\s*", '');
};

$(document).ready(function(){
	//判断是否保存密码
	username = $("#username").val();
	password = $("#password").val();
	if(username != null && username.trim() != "" && password != null & password.trim() != ""){
		$("#remenberMe").attr("checked","checked").val(1);
//		alert($("#remenber").val() +","+ username +","+ password);
	}
});

/**
 * 登录
 */
function login(obj){
	if(login_checkAll()){
		$("input button").attr("disabled","disabled");
		
		//表单提登录
//		$("form").submit();		
		//Ajax登录
//		alert(username +","+ password +","+ remenberMe);
		$.ajax({
			url:'login',
			data:{'user.username':username,'user.password':password,'remenberMe':remenberMe},
			type:'post',
			dataType:'json',
			beforeSend:function(){
				$(obj).text("正在登录...");
				$("#loading_img").css("display","inline");
			},
			complete:function(){
				$("#loading_img").css("display","none");
				$(obj).text("登录");
			},
			success:function(data){
				if(data.state == 1){
					window.location.href = "home";
				}else{
					$("#msg").text(data.responseMsg);
				}
			},
			error:function(){
				alert("请求失败！");
			}
		});
	}
}
/**
 * 登录验证
 * @returns {Boolean}
 */
function login_checkAll(){
	username = $("#username").val();
	password = $("#password").val();
	remenberMe = $("#remenberMe").val();
//	alert(remenber);
	if(username == null || username.trim() == ""){
		$("#msg").text("用户名不能为空");
		return false;
	}else if(password == null || password.trim() == ""){
		$("#msg").text("密码不能为空");
		return false;
	}else{
		return true;
	}
}

/**
 * 设置checkbox的value值：0-未选中；1-选中
 * @param obj
 */
function checkBox(obj){
//	var remenber = $(obj).attr("checked"); //这样是不对滴，次奥；正确代码如下
	if($(obj).prop("checked") == true){
		$(obj).val(1);
	}else{
		$(obj).val(0);
	}
}
